<?php

class Pinjaman extends Controller {

	function Pinjaman()
	{
		parent::Controller();	
		$this->load->library('auth');
		$this->auth->check_user_authentification();
		$this->load->model('pinjaman_model');					
		$this->load->helper('indodate');
		$this->load->helper('finance');
		$this->load->library('fpdf');
	}
	
	function index()
	{
		$data['title'] = "Daftar Pinjaman";
		$data['main_content'] = 'pinjaman/display';
		$data['pinjaman_data'] = $this->pinjaman_model->get_all_data();
		//$data['sisa_angsuran'] = $this->pinjaman_model->get_sisa_angsuran();
		$this->load->view('layout/template', $data);
	}
	
	function analisa()
	{
		$data['title'] = "Analisa Pinjaman";
		$data['main_content'] = 'pinjaman/display_analisa';
		$data['pinjaman_data'] = $this->pinjaman_model->get_all_data();
		$this->load->view('layout/template', $data);
	}	

	function add()
	{
		$data['title'] = "Tambah Pinjaman Harian";
		$data['main_content'] = 'pinjaman/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}
	
	function add_harian()
	{
		$data['title'] = "Tambah Pinjaman Harian";
		$data['main_content'] = 'pinjaman/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}
	
	function add_mingguan()
	{
		$data['title'] = "Tambah Pinjaman Mingguan";
		$data['main_content'] = 'pinjaman/form_mingguan';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}
	function add_bulanan()
	{
		$data['title'] = "Tambah Pinjaman Bulanan";
		$data['main_content'] = 'pinjaman/form_bulanan';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}
	
	function add_gadean()
	{
		$data['title'] = "Tambah Pinjaman Gadean";
		$data['main_content'] = 'pinjaman/form_gadean';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}
	function add_p3kum()
	{
		$data['title'] = "Tambah Pinjaman P3KUM";
		$data['main_content'] = 'pinjaman/form_p3kum';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['pinjaman_data'] = FALSE;
		$data['new_id'] = $this->pinjaman_model->get_last_id();
		$this->load->view('layout/template', $data);
	}

	function cetak_spk()
	{		
		$this->load->library('fpdf');
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$this->fpdf->FPDF("P","cm","A4");		
		$id = $this->uri->segment(3);	
		$data['laporan_data'] = $this->pinjaman_model->get_data_by_id($id);	
		//$data['saldo_data'] = $this->pinjaman_model->get_saldo_by_id($id);	
		$this->load->view('pinjaman/cetak_spk', $data);		
	}
	
	function view()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Lihat Pinjaman";
		$data['main_content'] = 'pinjaman/form';
		$data['act'] = 'view';
		$data['form_act'] = '';
		$data['pinjaman_data'] = $this->pinjaman_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}	

	function edit()
	{
		$id = $this->uri->segment(3);
		$data['pinjaman_data'] = $this->pinjaman_model->get_data_by_id($id);		
		$data['main_content'] = '';
		$jenis_pinj = $this->pinjaman_model->get_jenis_by_id($id);
		if($jenis_pinj->jenis_pinj == 1){
			$jenis_pinj = 'Jenis Pinjaman Harian';
			$data['main_content'] = 'pinjaman/form';
		}else if($jenis_pinj->jenis_pinj == 2){
			$jenis_pinj = 'Jenis Pinjaman Mingguan';
			$data['main_content'] = 'pinjaman/form_mingguan';
		}else if($jenis_pinj->jenis_pinj == 3){
			$jenis_pinj = 'Jenis Pinjaman Bulanan';
			$data['main_content'] = 'pinjaman/form_bulanan';
		}else if($jenis_pinj->jenis_pinj == 4){
			$jenis_pinj = 'Jenis Pinjaman Gadean';
			$data['main_content'] = 'pinjaman/form_gadean';
		}else if($jenis_pinj->jenis_pinj == 5){
			$jenis_pinj = 'Jenis Pinjaman P3 KUM';
			$data['main_content'] = 'pinjaman/form_p3kum';
		}
		$data['title'] = "$jenis_pinj";
		$data['act'] = 'edit';
		$data['form_act'] = 'update/'.$id;
		$this->load->view('layout/template', $data);		
	}
	
	function popup()
	{
		$data['pinjaman_data'] = $this->pinjaman_model->get_all_data();
		$this->load->view('pinjaman/popup', $data);
	}
	
	function insert()
	{
		if (!$this->_pinjaman_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->add();
		}
		else
		{
			$this->pinjaman_model->fill_data();
			//Cek Akun
			if(!$this->pinjaman_model->check_name())
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Rek pinjaman telah digunakan');
				$this->add();
			}
			//Insert Data
			elseif($this->pinjaman_model->insert_data()) 
			{
				$this->session->set_userdata('SUCCESSMSG', 'pinjaman baru sukses');
				redirect('pinjaman');
			}
		}
	}

	function update()
	{
		$id = $this->uri->segment(3);
		if (!$this->_pinjaman_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->edit();
		}
		else
		{
			$this->pinjaman_model->fill_data();
			//Cek Akun
			if(!$this->pinjaman_model->check_name($id))
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Rek pinjaman telah digunakan');
				$this->edit();
			}		
			//Update Data
			elseif($this->pinjaman_model->update_data($id))
			{
				$this->session->set_userdata('SUCCESSMSG', 'Update pinjaman sukses ');
				redirect('pinjaman');
			}
		}
	}

	function delete()
	{
		$id = $this->input->post('id');
		$pinjaman_data = $this->pinjaman_model->get_data_by_id($id);
		if($this->_check_jurnal_exist($id))
		{
			$msg = 'E#Pimpanan '.$pinjaman_data['no_rekening'].' tidak dapat dihapus karena telah dipakai di jurnal.';
		}
		else
		{
			if($this->pinjaman_model->delete_data($id))
			{
				$msg = 'S#Pinjaman '.$pinjaman_data['no_rekening'].' telah dihapus.';
			}
			else
			{
				$msg .= 'E#Terjadi kesalahan dalam menghapus data pinjaman '.$pinjaman_data['no_rekening'].'. Harap coba lagi.';
			}
		}
		echo $msg;
	}
	
	function _check_jurnal_exist($id)
	{
		$this->load->model('jurnal_model');
		$this->jurnal_model->set_project(0,$id);
		$journal_data = $this->jurnal_model->get_data();
		if ($journal_data)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function _pinjaman_validation()
	{
		$this->form_validation->set_rules('anggota', 'Anggota', 'trim|required');
			
		return $this->form_validation->run();
	}

}
/* End of file pinjaman.php */
/* Location: ./application/controllers/pinjaman.php */
